package com.example.preferencescreenexample;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Context mContext;
	private SharedPreferences mPrefs;
	private TextView mTextView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mContext = this;
		mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
	}

	@Override
	protected void onResume(){
		super.onResume();
		
		//Display the text values of all the options
		String textOut = "";
		
		textOut += "Welcome ";
		textOut += mPrefs.getString("text_name_option", "");
		
		textOut += "\n\n\nYour preferences:";
		textOut += "\n\ncheck_option_1: ";
		textOut += mPrefs.getBoolean("check_option_1", false);
		
		textOut += "\n\ncheck_option_2: ";
		textOut += mPrefs.getBoolean("check_option_2", false);
		
		//If the user setup the color preference, 
		//then color will not be an empty String
		String color = mPrefs.getString("text_color_option","");
		if(!color.isEmpty()){
			textOut += "\n\nYour favorite color is " + color;
		}
		
		mTextView = (TextView) findViewById(R.id.text_output);
		
		mTextView.setText(textOut);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//If the "Settings" button is pressed,
		//go to the PreferenceActivity
		if(item.getItemId() == R.id.action_settings){
			Intent i = new Intent(mContext, PreferenceActivity.class);
			startActivity(i);
		}
		return super.onOptionsItemSelected(item);
	}

}
